package engine

import (
	"fmt"
	"miniDW/src/log"
)

const (
	CONNST_STOP    = 0
	CONNST_RUNNING = 1
	CONNST_CLOSED  = 2
)

const (
	CMD_NOSOURCE    = -1
	CMD_STOP        = 0
	CMD_RUNNING     = 1
	CMD_TABLESTRUCT = 2
	CMD_ROWDATA     = 3
	CMD_EOF         = 4
)

type CommandMsg struct {
	Cmd    int
	Source string
	Data   interface{}
}

type Connector struct {
	source []string
	target string

	status int

	cmdCh chan CommandMsg
}

func NewConnector(target string) *Connector {
	connector := &Connector{
		target: target,
	}
	connector.cmdCh = make(chan CommandMsg, 10)
	return connector
}

func (connector *Connector) Source() []string {
	return connector.source
}

func (connector *Connector) Target() string {
	return connector.target
}

func (connector *Connector) Close() error {
	log.Debug(fmt.Sprintf("close connector: %s-%s", connector.source, connector.target))
	if connector.cmdCh != nil {
		close(connector.cmdCh)
	}
	return nil
}

func (connector *Connector) Received() CommandMsg {
	return <-connector.cmdCh
}

func (connector *Connector) Send(msg CommandMsg) {
	go func() { connector.cmdCh <- msg }()
}
